(function(exports,$) {
    function refresh(){     // 刷新函数
        capRefresh();
    };
    function destroy(){     // 销毁   
        capDestroy();
    };
    var Options = {     // 验证码参数
        'popup' : {
            type : "popup", // 弹窗类型
            disturbLevel: 1,
			isHttps: 0,
			clientType: 1,
			businessId: 0,
            callback : popupCB,
            end : popupEnd
        }
    }
    function popupCB(retJson) {     // 验证码回调
        if (retJson.ret == 0) {
            // 用户验证成功
            // document.getElementById("captcha").value = retJson.ticket;
            // document.getElementById("myform").submit();
            getSms(retJson.ticket)
        }
        else {
            //用户关闭验证码页面，没有验证
        }
    }
    function popupEnd(ret) {    // 验证结束回调
        $('.toast').hide();
    }
    function init(id,type) {    // 调用验证码
        capInit(document.getElementById(id),Options[type]);
    }
    function loadJs(url , callback){    //加载js
        var node = document.createElement("script");
        node[window.addEventListener ? "onload":"onreadystatechange"] = function(){
            if(window.addEventListener || /loaded|complete/i.test(node.readyState)){
        　　　　callback();
        　　　　node.onreadystatechange = null;
        　　}
        }
        node.onerror = function(){};
        node.src = url;
        var head = document.getElementsByTagName("head")[0];
        head.insertBefore(node,head.firstChild); 　
    }
        //发送短信验证码
function getSms (ticket) {
    var	phone = $('#phone').val();
	var params = {
		'method': 'sendSmsCodeWithCaptcha',
		'phone': phone,
		'ext': 1,
		'captcha': ticket,
		'subtime': new Date().getTime()
	};
	Webapp.postLoadData('/business.do', params, function(data) {
		auto();
		smsRandom = data.smsRandom;
	}, function(e) {
		if (e.desc) {
			destroy();
			$('.error').html(e.desc).show();
			return false;
		}
	});
	var totaltime = 120;
	function auto() {
	    var $idCode = $("#idCode");
		if (totaltime > 0) {
			totaltime--;
			if (!$idCode.hasClass('no_bg')) {
                $idCode.css("background", "rgb(182, 184, 185)");
            }
            $idCode.val(totaltime + ' 秒').attr('disabled', 'disabled').css({
				'cursor': 'default'
			});
			setTimeout(function() {
				auto();
			}, 1000);
		} else {
            if (!$idCode.hasClass('no_bg')) {
                $idCode.css("background", "#ff933c");
            }
            $idCode.addClass('flagCode');
            $idCode.val('重新获取验证码').removeAttr('disabled').css({
				'cursor': 'pointer'
			});
			totaltime = 120;
		};
	};
}
    $(function(){
        Webapp.postLoadData('/business.do', {
        method: 'getCaptchaJsUrl',
        subtime: new Date().getTime(),
        debug: 1
    }, function(data){
        if (data.status === 200) {
            loadJs(data.result, fCb);
        }
    });
    function fCb(){
        $("#idCode").click(function() {
            $('#sub_rapid').removeAttr("disabled");
            flag = true;
            var errmess = "";
            var	phone = $('#phone').val();
            var reg = /^[0-9a-zA-Z]{4}|[0-9]{6}$/;
            if (phone == '' || phone == null) {
                errmess = "请填写手机号码";
                $('.error').html(errmess).show();
                return false;
            } else if (!rapidCheck._checkPhone(phone)) {
                errmess = "请填写正确的手机号码";
                $('.error').html(errmess).show();
                return false;
            } else if (flag) {
                refresh();
                $('.toast').show();
                init("TCaptcha","popup");
            } else {
                errmess = "请输入正确的手机号";
                $('.error').html(errmess).show();
                $('#phone').focus();
                return false;
            }
        });
    }

})
})(window,$,undefined)
    